#ifndef CLOUDMACHINESMANAGER_H
#define CLOUDMACHINESMANAGER_H

#include <set>
#include <map>
#include <string>
#include <ctime>
#include <pthread.h>
#include "ServerCommon.h"

struct Machine {
	time_t alive;
	char hostname[CMM_MESS_LEN];
	int CPUNum;
	double CPUPower;
	int GPUNum;
	double GPUPower;
	pthread_mutex_t *end_lock;
};

class CloudMachinesManager {
public:
	// 开一个新线程接受心跳
	CloudMachinesManager();
	// 分配机器
	std::map<int, struct Machine> allocMachines(int num);
	// 释放机器
	void freeMachines(std::map<int, struct Machine> machines);
	std::string getResult(int num);
	// 结束线程,释放资源
	~CloudMachinesManager();
private:
	// 接受维护信息用函数,用epoll轮询
	static void* recvMessage(void *args);
	static std::map<int, std::string> results;
	static pthread_mutex_t results_mutex;
	// 节点池,需要锁
	static std::set<int> freePool;
	static pthread_mutex_t freePool_mutex;
	// 节点池,需要锁
	static std::set<int> busyPool;
	static pthread_mutex_t busyPool_mutex;
	// 节点信息,需要锁
	static std::map<int ,struct Machine> machineInfo;
	static pthread_mutex_t machineInfo_mutex;
	// 线程结束变量,需要锁
	static bool threadEnd;
	static pthread_mutex_t threadEnd_mutex;
	// 线程标示
	static pthread_t tidp;
	static int ref_cnt;
};

#endif
